/*
 * Copyright (C) 2009 Chang Min Lee <ch5ngmn@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. (/COPYING)
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#ifndef TAONET_PROXY_MANAGER_H
#define TAONET_PROXY_MANAGER_H

#include "ace/Hash_Map_Manager.h"
#include "ace/Malloc_T.h"
#include "ace/Null_Mutex.h"

#include "tao/ORB_Core.h"
#include "tao/PortableServer/PortableServer.h"
#include "orbsvcs/CosNamingC.h"
#include "ace/Refcounted_Auto_Ptr.h"
#include "ServerFinder.h"
#include "ClientId.h"

class ServerProxy;
class IoEventHandler;

class ProxyManager
{
public:
	ProxyManager (const ACE_TCHAR *id, 
												IoEventHandler *handler,
												CORBA::ORB_ptr orb, 
												CosNaming::NamingContextExt_ptr nctx);
	~ProxyManager();

	int open ();

	void close(void);
  
	int process (const ACE_TCHAR * server_name, const ACE_UINT8 *data, ACE_UINT32 data_len);

private:
	int connect (const ACE_TCHAR *server_name, const ACE_TCHAR *id);

private:
	typedef const ACE_TCHAR* KEY;
	typedef ServerProxy* VALUE;
	typedef ACE_Hash_Map_Manager_Ex<KEY, VALUE,
																										ACE_Hash<const ACE_TCHAR*>,
																										ACE_Equal_To<const ACE_TCHAR*>,
																										ACE_Null_Mutex> SERVER_LIST;
	SERVER_LIST server_list_;

	ClientId id_;

  CORBA::ORB_ptr orb_;
  PortableServer::POA_var root_poa_;
	PortableServer::POAManager_var poa_manager_;
	CosNaming::NamingContextExt_ptr naming_;

	ACE_Refcounted_Auto_Ptr <ServerFinder, ACE_Null_Mutex> server_finder_;
	IoEventHandler *handler_;
	bool is_open_;
};
#endif